//
//  LSZAnswerCollectionViewCell.m
//  类似答题
//
//  Created by 九酷 on 2018/9/4.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZAnswerCollectionViewCell.h"
@implementation LSZAnswerCollectionViewCell
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.labelType = [[UILabel alloc]init];
        if (iPhoneX) {
            self.labelType.frame = CGRectMake(15, 20, ScreenWidth-30, 20);
        }else{
            self.labelType.frame = CGRectMake(15, 10, ScreenWidth-30, 20);
        }
        self.labelType.textColor = RGBColor(0, 187, 236);
        self.labelType.font = FONT_(15);
        [self addSubview:self.labelType];
        if ([self.manager.examType isEqualToString:@"1"]) {
            self.labelType.text = @"[单选题]";
        }else if ([self.manager.examType isEqualToString:@"0"]){
            self.labelType.text = @"[判断题]";
        }else{
            self.labelType.text = @"[多选题]";
        }
        
        NSString * string = [NSString stringWithFormat:@"%ld: %@",(long)self.numberRow,self.manager.name];
        
        CGFloat height = [LSZSpecialPackaging getHeightOfText:ScreenWidth-30 height:ScreenHeight font:16 :string];
        self.titleL = [[UILabel alloc]init];
        self.titleL.frame = CGRectMake(15, 30, ScreenWidth - 30, height);
        self.titleL.font = [UIFont systemFontOfSize:16];
        self.titleL.numberOfLines = 0;
        self.titleL.text = string;
        self.titleL.textColor = titleColorLSZ;
        [self.contentView addSubview:self.titleL];

        self.examTableV = [[UITableView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(self.titleL.frame), ScreenWidth, ScreenHeight - 64 - 50 - 70) style:(UITableViewStylePlain)];
        [self.contentView addSubview:self.examTableV];
        self.examTableV.delegate = self;
        self.examTableV.dataSource = self;
        self.examTableV.scrollEnabled = NO;
        self.examTableV.backgroundColor = RGBColor(236, 236, 236);
        self.examTableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.examTableV registerClass:[AnswerCell class] forCellReuseIdentifier:@"AnswerCell"];
        [self.examTableV registerClass:[LSZRightAndWrongTableViewCell class] forCellReuseIdentifier:@"LSZRightAndWrongTableViewCell"];
    }
    return self;
}
-(void)setManager:(LSZAnswerQuestionsDetailsMg *)manager
{
    if (_manager != manager) {
        _manager = nil;
        _manager = manager;
        if ([self.manager.examType isEqualToString:@"1"]) {
            self.labelType.text = @"[单选题]";
        }else if ([self.manager.examType isEqualToString:@"0"]){
            self.labelType.text = @"[判断题]";
        }else{
            self.labelType.text = @"[多选题]";
        }
        NSString * string = [NSString stringWithFormat:@"%ld: %@",(long)self.numberRow,self.manager.name];
        CGFloat height = [LSZSpecialPackaging getHeightOfText:ScreenWidth-30 height:ScreenHeight font:16 :string];
        self.titleL.text = string;
        CGRect frame = self.titleL.frame;
        frame.size.height = height;
        self.titleL.frame = frame;
        
        CGRect frameOne = self.examTableV.frame;
        frameOne.origin.y = CGRectGetMaxY(self.titleL.frame);
        self.examTableV.frame = frameOne;
        
        [self.examTableV reloadData];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.manager.examType isEqualToString:@"0"]) {
        return 1;
    }else{
        return self.manager.arrayContent.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.manager.examType isEqualToString:@"0"]) {
        LSZRightAndWrongTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZRightAndWrongTableViewCell" forIndexPath:indexPath];
        cell.labelYes.text = self.manager.arrayContent[0];
        cell.labelNo.text = self.manager.arrayContent[1];
//        NSLog(@"%ld>>>>>判断题",(long)self.manager.number);
        if (self.manager.number == -1) {
            [cell.butYes setImage:[UIImage imageNamed:@"党建_未选中"] forState:UIControlStateNormal];
            [cell.butNo setImage:[UIImage imageNamed:@"党建_未选中"] forState:UIControlStateNormal];
        }else  if (self.manager.number == 0) {
            [cell.butYes setImage:[UIImage imageNamed:@"党建_选中"] forState:UIControlStateNormal];
            [cell.butNo setImage:[UIImage imageNamed:@"党建_未选中"] forState:UIControlStateNormal];
        }else{
            [cell.butYes setImage:[UIImage imageNamed:@"党建_未选中"] forState:UIControlStateNormal];
            [cell.butNo setImage:[UIImage imageNamed:@"党建_选中"] forState:UIControlStateNormal];
        }
        [cell.butYes addTarget:self action:@selector(clickYes) forControlEvents:UIControlEventTouchUpInside];
        [cell.butNo addTarget:self action:@selector(clickNo) forControlEvents:UIControlEventTouchUpInside];
        cell.contentView.backgroundColor = RGBColor(236, 236, 236);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        return [self chooseTableView:tableView cellForRowAtIndexPath:indexPath];
    }
}
- (UITableViewCell *)chooseTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AnswerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AnswerCell" forIndexPath:indexPath];
    if ([self.manager.examType isEqualToString:@"1"]) {
        //单选
        if (self.manager.number == indexPath.row) {
            cell.contentL.textColor =themeColor;
            [cell.choseBtn setImage:[UIImage imageNamed:@"党建_选中"] forState:(UIControlStateNormal)];
        }else{
            cell.contentL.textColor = titleColorLSZ;
            [cell.choseBtn setImage:[UIImage imageNamed:@"党建_未选中"] forState:(UIControlStateNormal)];
        }
    }
    else{
        //多选
        NSArray * array = self.manager.addAnswerArray;
        if (array.count>0) {
            NSString * a = @"0";
            for (int i = 0; i<array.count; i++) {
                NSInteger strRow = [array[i] integerValue];
                if (strRow == indexPath.row) {
                    a = @"1";
                }
            }
            if ([a isEqualToString:@"1"]) {
                cell.contentL.textColor = themeColor;
                [cell.choseBtn setImage:[UIImage imageNamed:@"党建_选中"] forState:(UIControlStateNormal)];
            }else{
                cell.contentL.textColor = titleColorLSZ;
                [cell.choseBtn setImage:[UIImage imageNamed:@"党建_未选中"] forState:(UIControlStateNormal)];
            }
            
        }else{
            cell.contentL.textColor = titleColorLSZ;
            [cell.choseBtn setImage:[UIImage imageNamed:@"党建_未选中"] forState:(UIControlStateNormal)];
        }
    }
    NSArray * arrayM =[NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z", nil];
    cell.strTitle = [NSString stringWithFormat:@"%@,%@",arrayM[indexPath.row],self.manager.arrayContent[indexPath.row]];
    cell.contentL.text = cell.strTitle;
    cell.contentView.backgroundColor = RGBColor(236, 236, 236);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark - 是非题选项
-(void)clickYes
{
    LSZRightAndWrongTableViewCell * cell = (LSZRightAndWrongTableViewCell *)[self.examTableV cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0  inSection:0]];
    [cell.butYes setImage:[UIImage imageNamed:@"党建_选中"] forState:UIControlStateNormal];
    [cell.butNo setImage:[UIImage imageNamed:@"党建_未选中"] forState:UIControlStateNormal];
    self.manager.number = 0;

}
-(void)clickNo
{
    LSZRightAndWrongTableViewCell * cell = (LSZRightAndWrongTableViewCell *)[self.examTableV cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0  inSection:0]];
    [cell.butYes setImage:[UIImage imageNamed:@"党建_未选中"] forState:UIControlStateNormal];
    [cell.butNo setImage:[UIImage imageNamed:@"党建_选中"] forState:UIControlStateNormal];
    self.manager.number = 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.examTableV deselectRowAtIndexPath:indexPath animated:YES];
    AnswerCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([self.manager.examType isEqualToString:@"0"]) {
        return;
    }else{
        if ([self.manager.examType isEqualToString:@"1"]) {
            //单选
            self.manager.number = indexPath.row;
            [self.examTableV reloadData];
            
            //对答题进行存储
            /**
             *  单选: 这里每答一道题 进行保存 本地或者服务器
             */
        }
        else{
            NSArray * array = self.manager.addAnswerArray;
            NSLog(@"添加的选项有>>%@",array);
            if (array.count>0) {
                int A = -1;
                for (int i = 0; i<array.count; i++) {
                    NSInteger strRow = [array[i] integerValue];
                    if (strRow == indexPath.row) {
                        A = i;
                    }
                }
                if (A  == -1) {//次选项没有添加过
                    [self.manager.addAnswerArray addObject:@(indexPath.row)];
                    NSLog(@"添加>>%ld",(long)indexPath.row);
                }else{
                    //次选项添加过
                    [self.manager.addAnswerArray removeObjectAtIndex:A];
                    NSLog(@"移除>>%ld",(long)indexPath.row);
                }
                NSLog(@"添加的选项有>>%@",self.manager.addAnswerArray);
                [self.examTableV reloadData];
            }else{
                [self.manager.addAnswerArray  addObject:@(indexPath.row)];
                [self.examTableV reloadData];
            }
        }
    }
}
@end
